//
//  AlbumToolbar.m
//  ArtDog
//
//  Created by dllo on 16/9/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AlbumToolbar.h"
#import "BaseCollectionView.h"
#import "HLMacro.h"
#import "ImageViewCollectionCell.h"
#import <Photos/Photos.h>
#import "UIColor+Common.h"

@interface AlbumToolbar ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UILabel *supIndexLabel;

@property (nonatomic, strong) PHImageManager *imageManager;

@end

@implementation AlbumToolbar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (NSMutableArray *)dataArray
{
    _imageManager = [[PHImageManager alloc]init];
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    if (_dataArray.count != 0) {
        _supIndexLabel.hidden = NO;
        _finishBtn.enabled = YES;
        [_finishBtn setTitleColor:[UIColor colorFromHexRGB:@""] forState:UIControlStateNormal];
        _supIndexLabel.text = [NSString stringWithFormat:@"%ld", _dataArray.count];
    } else {
        _supIndexLabel.hidden = YES;
        _finishBtn.enabled = NO;
        [_finishBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    [_collectionView reloadData];
    return _dataArray;
}

- (void)setupViews
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH - 90, self.frame.size.height - 10) collectionViewLayout:layout];
    [self addSubview:_collectionView];
    layout.itemSize = CGSizeMake(self.frame.size.height - 10, self.frame.size.height - 10);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView.showsHorizontalScrollIndicator = NO;
    layout.minimumLineSpacing = 3;
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerNib:[UINib nibWithNibName:@"ImageViewCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"imageViewCollectionCell"];
    
    _finishBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 70, 10, 60, 30)];
    [self addSubview:_finishBtn];
    [_finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    _finishBtn.enabled = NO;
    [_finishBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    _supIndexLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 25, 10, 10, 10)];
    [self addSubview:_supIndexLabel];
    _supIndexLabel.backgroundColor = [UIColor redColor];
    _supIndexLabel.layer.cornerRadius = 5;
    _supIndexLabel.hidden = YES;
    _supIndexLabel.clipsToBounds = YES;
    _supIndexLabel.textColor = [UIColor whiteColor];
    _supIndexLabel.font = [UIFont systemFontOfSize:10];
    _supIndexLabel.textAlignment = NSTextAlignmentCenter;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageViewCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageViewCollectionCell" forIndexPath:indexPath];
    
    PHAsset *asset = _dataArray[indexPath.item];
    [_imageManager requestImageForAsset:asset targetSize:cell.frame.size contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        
        if (result) {
            cell.imageView.image = result;
        }
    }];
    return cell;
}

@end
